class RefundApplication < ApplicationRecord
  belongs_to :member
  enum operator: [:admin, :member, :suspend]
  enum status: [:pending, :refused, :agreed]

  scope :ct_order, -> {
    select(
        :id,
        :order_id,
        :amount,
        :order_type,
        'orders.pay_type AS pay_type',
        'orders.source AS order_source',
        "members.name AS member_name",
        :operator,
        :status,
        :created_at,
        :updated_at
    ).joins(:member).joins("LEFT JOIN orders ON orders.order_no = refund_applications.order_id").where(order_type: 'Order')}
  scope :cp_order, -> {select(
      :id,
      :order_id,
      :amount,
      :order_type,
      'camp_orders.pay_type AS pay_type',
      'members.name AS member_name',
      "'微信' AS order_source",
      :operator,
      :status,
      :created_at,
      :updated_at
  ).joins(:member).joins("LEFT JOIN camp_orders ON camp_orders.order_no = refund_applications.order_id").where(order_type: 'CampOrder')}
  scope :tr_order, -> {select(
      :id,
      :order_id,
      :amount,
      :order_type,
      "CASE
       WHEN coach_orders.pay_type = 1 then '支付宝'
       WHEN coach_orders.pay_type = 2 then '微信'
       ELSE '其他'
       END as pay_type",
      'members.name AS member_name',
      "'爱动教练端' AS order_source",
      :operator,
      :status,
      :created_at,
      :updated_at
  ).joins(:member).joins("LEFT JOIN coach_orders ON coach_orders.order_no = refund_applications.order_id").where(order_type: 'CoachOrder')}
  scope :campaign_order, -> {select(
      :id,
      :order_id,
      :amount,
      :order_type,
      :pay_type,
      'members.name AS member_name',
      "'微' AS order_source",
      :operator,
      :status,
      :created_at,
      :updated_at
  ).joins(:member).joins("LEFT JOIN campaigns_orders ON campaigns_orders.order_no = refund_applications.order_id").where(order_type: 'Campaigns::Order')}
end
